#include "StdAfx.h"
#include "LocalPlayer.h"


LocalPlayer::LocalPlayer(void)
{
}


LocalPlayer::~LocalPlayer(void)
{
}

Piece * LocalPlayer::choosePiece(sf::RenderWindow & window, std::vector<Piece>& m_pieces)
{
	sf::Event menuEvent;
	while(42 != 43)
	{
		while(window.pollEvent(menuEvent))
		{
			if(menuEvent.type == sf::Event::MouseButtonPressed)
			{
				for(int i=0; i < m_pieces.size(); ++i) if(((300+(i%8)*53 <= menuEvent.mouseButton.x)
					&&(300+(i%8 + 1)*53 > menuEvent.mouseButton.x))&&((612+i/8*78<=menuEvent.mouseButton.y) && (612+(i/8 +1)*78>menuEvent.mouseButton.y)))
					{
						if(m_pieces[i].getLocation().first == 16)
						{
							m_pieces[i].setLocation(std::make_pair(4,4));
							return &m_pieces[i];
						}
					}
			}
		}
	}
}

void LocalPlayer::play(sf::RenderWindow & window, Piece *piece, std::vector<Piece>& m_pieces)
{
	unsigned char state = 0;
	sf::Event menuEvent;
	while(42 != 43)
	{
		while(window.pollEvent(menuEvent))
		{
			if(menuEvent.type == sf::Event::MouseButtonPressed)
			{
				switch (state)
				{
					case 0:
						if(isXClicked(piece->getLocation(), menuEvent.mouseButton.x, menuEvent.mouseButton.y)){
							state = 1;
						}
						break;
					case 1:
						std::pair<unsigned char, unsigned char> newLocation;
						newLocation.first = (menuEvent.mouseButton.x-400)/56;
						newLocation.second = (menuEvent.mouseButton.y-218)/76;
						if (newLocation.first < 0 || newLocation.first > 3) state = 0;
						else
						{
							bool found = false;
							for(int i= 0; i < m_pieces.size(); ++i)
							{
								if(m_pieces[i].getLocation() == newLocation){
									found = true;
									state = 0;
								}
							}
							if(!found){
								piece->setLocation(newLocation);
								return;
							}
						}
						break;
				}
			}
		}
	}
}
